package io.bdmc.modules.bss.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.bdmc.common.annotation.SysLogger1;
import io.bdmc.common.utils.Constant.ModifyName;
import io.bdmc.common.utils.R;
import io.bdmc.modules.bss.entity.DeviceEquipmentroom;
import io.bdmc.modules.bss.entity.DeviceFrame;
import io.bdmc.modules.bss.service.IDeviceEquipmentroomService;
import io.bdmc.modules.bss.service.IDeviceFrameService;
import io.bdmc.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 机房 前端控制器
 * </p>
 *
 * @author ping
 * @since 2020-06-29
 */

// "机房", description = "/bss/equipmentroom")
@RestController
@RequestMapping("/bss/equipmentroom")
public class DeviceEquipmentroomController extends AbstractController {

	@Autowired
	IDeviceEquipmentroomService _deviceequipmentroomSvs;

	@Autowired
	IDeviceFrameService _deviceframeSvs;

	@GetMapping("/list")
	// "查询当前部门DeviceEquipmentroom", notes = "查询当前部门机房")
	// @RequiresPermissions("bss:deviceEquipmentroom:list")
	public R list() {
		List<DeviceEquipmentroom> map = _deviceequipmentroomSvs.listByDeptId(getDeptId().intValue());
		return R.ok().putData(map);

	}

	@GetMapping("/info")
	// "查询单个机房", notes = "查询单个机房")
	public R info(int equipmentroomId) {

		DeviceEquipmentroom room = _deviceequipmentroomSvs.getById(equipmentroomId);
		return R.ok().putData(room);
	}

	@SysLogger1(value = "新增机房", entityType = DeviceEquipmentroom.class, operationType = ModifyName.ADD)
	@PostMapping("/save")
	// "新增DeviceEquipmentroom", notes = "新增机房")
	@RequiresPermissions("bss:equipmentroom:save")
	public R save(@RequestBody DeviceEquipmentroom deviceequipmentroom) {
		deviceequipmentroom.setCreateBy(getUser().getUsername());
		deviceequipmentroom.setCreateTime(LocalDateTime.now());
		Boolean b = deviceequipmentroom.insert();
		return R.trueOrFalse(b);
	}

	@SysLogger1(value = "修改机房", entityType = DeviceEquipmentroom.class, operationType = ModifyName.UPDATE)
	@PostMapping("/update")
	// "修改DeviceEquipmentroom", notes = "修改机房")
	@RequiresPermissions("bss:equipmentroom:update")
	public R update(@RequestBody DeviceEquipmentroom deviceequipmentroom) {
		Boolean b = deviceequipmentroom.updateById();
		return R.trueOrFalse(b);
	}

	@SysLogger1(value = "删除机房", entityType = DeviceEquipmentroom.class, operationType = ModifyName.DELETE)
	@PostMapping("/delete")
	// "删除DeviceEquipmentroom", notes = "删除机房")
	@RequiresPermissions("bss:equipmentroom:delete")
	public R delete(@RequestBody long equipmentroomId) {
		if (equipmentroomId > 0) {
			long devCount = _deviceframeSvs
					.count(new QueryWrapper<DeviceFrame>().eq("equipmentroom_id", equipmentroomId));
			if (devCount > 0) {
				return R.error(-1, "请先删除机房的机架。");
			}
			_deviceequipmentroomSvs.removeById(equipmentroomId);
		}

		return R.ok();
	}
}
